/**
 *
 */
package org.jprobe.classpath;

/**
 * represents a class entry read from the file system, or a jar archive
 * @author Hussain Al-Mutawa
 * @version 1.0
 * @since 1.0
 */
public class ClassEntry implements ClassEntryInterface {
	/** class name **/
	private transient String className;
	/** package name **/
	private transient String packageName;
	/** class path of the class entry **/
	protected final String classPath;
	public ClassEntry(final String classPath,final Class<?>clazz) {
		super();
		this.classPath=classPath;
		this.packageName = clazz.getPackage()==null?"":clazz.getPackage().getName();
		this.className   = clazz.getSimpleName();
	}
	/**
	 * instantiate new class entry from path
	 * @param path the path of the class in the file system
	 */
	public ClassEntry(final String classPath,final String path) {
		super();
		this.classPath=classPath;
		final int last = (path.lastIndexOf("/")==-1?path.lastIndexOf("."):path.lastIndexOf("/"));
		this.packageName = path.substring(0,last==-1?0:last).replaceAll("\\/", ".");
		this.className   = path.substring(last+1).replaceAll("\\$", ".").replace(".class", "");
		while(packageName.startsWith(".")){
			packageName=packageName.substring(1);
		}
	}
	@Override
	public String getClassName() {
		return className;
	}
	@Override
	public String getPackageName() {
		return packageName;
	}
	@Override
	public String getClasspath() {
		return classPath;
	}
	@Override
	public String toString() {
		return packageName+'.'+className;
	}
}
